package GUI.Ventas;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import sale.exceptions.NoSalesException;
import service.article.exceptions.ProxyException;

public class MostradorVentaDelivery extends Dialog {

	protected Object result;
	protected Shell shell;
	int op;
	Display display ;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public MostradorVentaDelivery(Shell parent, int style, int opcion) {
		super(parent, style);
		setText("SWT Dialog");
		op=opcion;
		if(opcion == 0){
			setText("Tipo de nueva Venta");
		}
		if (opcion ==1){
			setText("Ventas abiertas de:");
		}
		if(opcion == 2){
			setText ("Cerrar venta de");
		}
		if(opcion == 3){
			setText ("Seleccione el tipo de venta abierta");
		}
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		display= getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(380, 262);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		Button btnMostrador = new Button(shell, SWT.NONE);
		btnMostrador.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(op== 0){
					AgregarVentaMostrador cerrar = new AgregarVentaMostrador(shell, shell.getStyle(), null);
					shell.setVisible(false);
					cerrar.open();
					
				}
				if(op==1){
					VentasAbiertasMostrador v = new VentasAbiertasMostrador (shell, shell.getStyle(),0);
					shell.setVisible(false);
					try {
						v.open();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoSalesException e1) {
					ExcepcionesMenuVentas ex = new ExcepcionesMenuVentas (shell, shell.getStyle(),"No hay ventas abiertas de mostrador");
					shell.setVisible(false);
					ex.open();
						e1.printStackTrace();
					}
				}
				if(op == 2){
					VentasAbiertasMostrador v = new VentasAbiertasMostrador(shell, shell.getStyle(), 2);
					shell.setVisible(false);
					try {
						v.open();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoSalesException e1) {
						ExcepcionesMenuVentas ex = new ExcepcionesMenuVentas (shell, shell.getStyle(),"No hay ventas abiertas de mostrador");
						shell.setVisible(false);
						ex.open();
						e1.printStackTrace();
					}
				}
				if (op==3){
					VentasAbiertasMostrador v = new VentasAbiertasMostrador (shell, shell.getStyle(),0);
					shell.setVisible(false);
					try {
						v.open();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoSalesException e1) {
						ExcepcionesMenuVentas ex = new ExcepcionesMenuVentas (shell, shell.getStyle(),"No hay ventas abiertas de mostrador");
						shell.setVisible(false);
						ex.open();
						e1.printStackTrace();
					}
				}
			}
		});
		FormData fd_btnMostrador = new FormData();
		fd_btnMostrador.top = new FormAttachment(0, 50);
		fd_btnMostrador.left = new FormAttachment(0, 141);
		fd_btnMostrador.right = new FormAttachment(0, 235);
		btnMostrador.setLayoutData(fd_btnMostrador);
		btnMostrador.setText("Mostrador");
		
		Button btnMesa = new Button(shell, SWT.NONE);
		btnMesa.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(op== 0){
					AgregarVentaMesa cerrar = new AgregarVentaMesa(shell, shell.getStyle(),null);
					shell.setVisible(false);
					cerrar.open();
					
				}
				if (op ==1){
					VentasAbiertasMesas vent = new VentasAbiertasMesas (shell, shell.getStyle(),0);
					shell.setVisible(false);
					
					try {
						vent.open();	
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoSalesException e1) {
						ExcepcionesMenuVentas ex = new ExcepcionesMenuVentas (shell, shell.getStyle(),"No hay ventas abiertas de mesa");
						shell.setVisible(false);
						ex.open();
						e1.printStackTrace();
					}
					
				}
				if(op == 2){
					CerrarVentaMesa1 c = new CerrarVentaMesa1(shell, shell.getStyle());
					shell.setVisible(false);
					try {
						c.open();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				if (op==3){
					
					VentasAbiertasMesas m = new VentasAbiertasMesas (shell, shell.getStyle(), 1);
					shell.setVisible(false);
					try {
						m.open();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoSalesException e1) {
						ExcepcionesMenuVentas ex = new ExcepcionesMenuVentas (shell, shell.getStyle(),"No hay ventas abiertas de mesa");
						shell.setVisible(false);
						ex.open();
						e1.printStackTrace();
					}
				}
			}
		});
		FormData fd_btnMesa = new FormData();
		fd_btnMesa.top = new FormAttachment(btnMostrador, 25);
		fd_btnMesa.left = new FormAttachment(btnMostrador, 0, SWT.LEFT);
		fd_btnMesa.right = new FormAttachment(100, -145);
		btnMesa.setLayoutData(fd_btnMesa);
		btnMesa.setText("Mesa");
		
		Button btnDelivery = new Button(shell, SWT.NONE);
		btnDelivery.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(op==0){
					NuevaVentaDelivery n = new NuevaVentaDelivery (shell, shell.getStyle(), null, null, null,null);
					shell.setVisible(false);
					n.open();
					
			}
				if (op ==1){
					VetnasAbiertasDelivery vent = new VetnasAbiertasDelivery (shell, shell.getStyle(),0);
					shell.setVisible(false);
					try {
						vent.open();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoSalesException e1) {
						ExcepcionesMenuVentas ex = new ExcepcionesMenuVentas (shell, shell.getStyle(),"No hay ventas abiertas de delivery");
						shell.setVisible(false);
						ex.open();
						e1.printStackTrace();
					}
						
				}
				if(op == 2){
					CerrarVentaDelivery v = new CerrarVentaDelivery(shell, shell.getStyle());
					shell.setVisible(false);
					try {
						v.open();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoSalesException e1) {
						ExcepcionesMenuVentas ex = new ExcepcionesMenuVentas (shell, shell.getStyle(),"No hay ventas abiertas de mostrador");
						shell.setVisible(false);
						ex.open();
						e1.printStackTrace();
					}
				}
				if(op==3){
					VetnasAbiertasDelivery v = new VetnasAbiertasDelivery(shell,shell.getStyle(),1);
					shell.setVisible(false);
					try {
						v.open();
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (NoSalesException e1) {
						ExcepcionesMenuVentas ex = new ExcepcionesMenuVentas (shell, shell.getStyle(),"No hay ventas abiertas de mostrador");
						shell.setVisible(false);
						ex.open();
						e1.printStackTrace();
					}
				}
				
			}
		});
		FormData fd_btnDelivery = new FormData();
		fd_btnDelivery.right = new FormAttachment(btnMostrador, 0, SWT.RIGHT);
		fd_btnDelivery.top = new FormAttachment(btnMesa, 25);
		fd_btnDelivery.left = new FormAttachment(btnMostrador, 0, SWT.LEFT);
		btnDelivery.setLayoutData(fd_btnDelivery);
		btnDelivery.setText("Delivery");

	}
}
